using MongoDB.Bson;
using MongoDB.Bson.Serialization;
using MongoDB.Driver.Core.Misc;

namespace HotChocolate.Data.MongoDb;

public sealed class MongoDbIncludeProjectionOperation : MongoDbProjectionDefinition
{
    private readonly string _path;

    public MongoDbIncludeProjectionOperation(
        string field)
    {
        _path = Ensure.IsNotNull(field, nameof(field));
    }

    public override BsonDocument Render(
        IBsonSerializer documentSerializer,
        IBsonSerializerRegistry serializerRegistry)
    {
        StringFieldDefinitionHelper.Resolve(
            _path,
            documentSerializer,
            out var resolvedFieldName,
            out var _);

        return new BsonDocument(resolvedFieldName ?? _path, 1);
    }
}
